{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using llama-parse with Elasticsearch vector database\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this notebook, we show a basic RAG-style example that uses `llama-parse` to parse a PDF document, store the corresponding document in the [Elasticsearch vector database](https://www.elastic.co/elasticsearch/vector-database), and perform some basic queries. The notebook is modeled after the quick start notebooks and hence is meant as a way of getting started with `llama-parse`, backed by a vector database."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Requirements\n",
    "\n",
    "- Get an Llama Cloud API key from https://cloud.llamaindex.ai\n",
    "- Get an OpenAI API key from https://openai.com\n",
    "- Get an Elasticsearch key from [Elastic Cloud](https://cloud.elastic.co/serverless-registration?onboarding_token=vectorsearch) with a free trial."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# First, install the required dependencies\n",
    "%pip install --quiet llama-index llama-parse llama-index-vector-stores-elasticsearch llama-index-llms-openai"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter your Llama Index Cloud API Key: ··········\n",
      "Enter your Elastic Cloud ID: elasticsearch-serverless:dXMtZWFzdC0xLmF3cy5lbGFzdGljLmNsb3VkJGE5YmM0NmVmZWQ2NzQ4NDNhOTkzOTE3NDljMTYyNTQ3LmVzJGE5YmM0NmVmZWQ2NzQ4NDNhOTkzOTE3NDljMTYyNTQ3Lmti\n",
      "Enter your Elasticsearch API Key: ··········\n",
      "Enter your OpenAI API Key: ··········\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import openai\n",
    "\n",
    "from getpass import getpass\n",
    "\n",
    "# Get all required API keys and parameters\n",
    "llama_cloud_api_key = getpass(\"Enter your Llama Index Cloud API Key: \")\n",
    "es_cloud_id = input(\"Enter your Elastic Cloud ID: \")\n",
    "es_api_key = getpass(\"Enter your Elasticsearch API Key: \")\n",
    "openai_api_key = getpass(\"Enter your OpenAI API Key: \")\n",
    "\n",
    "os.environ[\"LLAMA_CLOUD_API_KEY\"] = llama_cloud_api_key\n",
    "openai.api_key = openai_api_key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# llama-parse is async-first, running the sync code in a notebook requires the use of nest_asyncio\n",
    "import nest_asyncio\n",
    "\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Using llama-parse to parse a PDF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Download complete.\n"
     ]
    }
   ],
   "source": [
    "# Grab a PDF from Arxiv for indexing\n",
    "import requests\n",
    "\n",
    "# The URL of the file you want to download\n",
    "url = \"https://arxiv.org/pdf/1706.03762.pdf\"\n",
    "# The local path where you want to save the file\n",
    "file_path = \"./attention.pdf\"\n",
    "\n",
    "# Perform the HTTP request\n",
    "response = requests.get(url)\n",
    "\n",
    "# Check if the request was successful\n",
    "if response.status_code == 200:\n",
    "    # Open the file in binary write mode and save the content\n",
    "    with open(file_path, \"wb\") as file:\n",
    "        file.write(response.content)\n",
    "    print(\"Download complete.\")\n",
    "else:\n",
    "    print(\"Error downloading the file.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Started parsing the file under job_id cac11eca-bd8d-4ca9-8889-2f3904363116\n"
     ]
    }
   ],
   "source": [
    "from llama_parse import LlamaParse\n",
    "\n",
    "documents = LlamaParse(result_type=\"text\").load_data(file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.google.colaboratory.intrinsic+json": {
       "type": "string"
      },
      "text/plain": [
       "'     Forward\\n                                                                       Add & Norm\\n                                             Add & Norm                  Masked\\n                                              Multi-Head                Multi-Head\\n                                               Attention                 Attention\\n                                                 Input                   Output\\n                                              Embedding                Embedding\\n                                 Figure 1: The Transformer - model architecture.\\n\\n\\nThe Transformer follows this overall architecture using stacked self-attention and point-wise, fully\\nconnected layers for both the encoder and decoder, shown in the left and right halves of Figure 1,\\nrespectively.\\n\\n\\n3.1    Encoder and Decoder Stacks\\n\\n\\nEncoder:        The encoder is composed of a stack of N = 6 identical layers. Each layer has two\\nsub-layers. The first is a multi-head self-attention mechanism, an'"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Take a quick look at some of the parsed text from the document:\n",
    "documents[0].get_content()[10000:11000]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Storing into Elasticsearch Vector Database"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.vector_stores.elasticsearch import ElasticsearchStore\n",
    "\n",
    "es_store = ElasticsearchStore(\n",
    "    index_name=\"llama-parse-docs\",\n",
    "    es_cloud_id=es_cloud_id,  # found within the deployment page\n",
    "    es_api_key=es_api_key,  # create an API key within Kibana (Security -> API Keys)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.node_parser import SimpleNodeParser\n",
    "\n",
    "node_parser = SimpleNodeParser()\n",
    "\n",
    "nodes = node_parser.get_nodes_from_documents(documents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.embeddings.openai import OpenAIEmbedding\n",
    "from llama_index.core import VectorStoreIndex, StorageContext\n",
    "\n",
    "storage_context = StorageContext.from_defaults(vector_store=es_store)\n",
    "\n",
    "index = VectorStoreIndex(\n",
    "    nodes=nodes,\n",
    "    storage_context=storage_context,\n",
    "    embed_model=OpenAIEmbedding(api_key=openai_api_key),\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Simple RAG Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "query_engine = index.as_query_engine(similarity_top_k=15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "***********New LlamaParse+ Basic Query Engine***********\n",
      "Additive attention.\n"
     ]
    }
   ],
   "source": [
    "query = \"What is Multi-Head Attention also known as?\"\n",
    "\n",
    "response_1 = query_engine.query(query)\n",
    "print(\"\\n***********New LlamaParse+ Basic Query Engine***********\")\n",
    "print(response_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.google.colaboratory.intrinsic+json": {
       "type": "string"
      },
      "text/plain": [
       "'The two most commonly used attention functions are additive attention [ 2], and dot-product (multi-\\nplicative) attention. Dot-product attention is identical to our algorithm, except for the scaling factor\\n    √1dk. Additive attention computes the compatibility function using a feed-forward network with\\nof\\na single hidden layer. While the two are similar in theoretical complexity, dot-product attention is\\nmuch faster and more space-efficient in practice, since it can be implemented using highly optimized\\nmatrix multiplication code.\\nWhile for small values of dk the two mechanisms perform similarly, additive attention outperforms\\ndot product attention without scaling for larger values of dk [3 ]. We suspect that for large values of\\ndk, the dot products grow large in magnitude, pushing the softmax function into regions where it has\\nextremely small gradients4. To counteract this effect, we scale the dot products by                            √1dk.\\n\\n\\n\\n\\n\\n\\n3.2.2     Multi-Head Attention\\nInstead of performing a single attention function with dmodel-dimensional keys, values and queries,\\nwe found it beneficial to linearly project the queries, keys and values h times with different, learned\\nlinear projections to dk, dk and dv dimensions, respectively. On each of these projected versions of\\nqueries, keys and values we then perform the attention function in parallel, yielding dv -dimensional\\nvariables with mean 0 and variance 1. Then their dot product, q · k = Pdk\\n                                                                         4To illustrate why the dot products get large, assume that the components of q and k are independent random\\n                                                                                       i=1 qiki, has mean 0 and variance dk .\\n\\n\\n                                                                 4\\n---\\n output values. These are concatenated and once again projected, resulting in the final values, as\\n depicted in Figure 2.\\n Multi-head attention allows the model to jointly attend to information from different representation\\n subspaces at different positions. With a single attention head, averaging inhibits this.\\n\\n\\n                                 MultiHead(Q, K, V ) = Concat(head1, ..., headh)W O\\n                                                where headi = Attention(QW i Q, KW i K, V W i V)\\n\\n\\nWhere the projections are parameter matrices W i ∈ Rdmodel×dk , W i ∈ Rdmodel×dk , W i ∈ Rdmodel×dvQ        K  V\\n and W O ∈ Rhdv ×dmodel .\\n\\n\\n In this work we employ h = 8 parallel attention layers, or heads. For each of these we use\\n dk = dv = dmodel/h = 64. Due to the reduced dimension of each head, the total computational cost\\n is similar to that of single-head attention with full dimensionality.\\n\\n\\n 3.2.3       Applications of Attention in our Model\\nThe Transformer uses multi-head attention in three different ways:\\n\\n\\n            • In \"encoder-decoder attention\" layers, the queries come from the previous decoder layer,\\n               and the memory keys and values come from the output of the encoder. This allows every\\n               position in the decoder to attend over all positions in the input sequence. This mimics the\\n               typical encoder-decoder attention mechanisms in sequence-to-sequence models such as\\n               [38, 2, 9].\\n            • The encoder contains self-attention layers. In a self-attention layer all of the keys, values\\n               and queries come from the same place, in this case, the output of the previous layer in the\\n               encoder. Each position in the encoder can attend to all positions in the previous layer of the\\n               encoder.\\n            • Similarly, self-attention layers in the decoder allow each position in the decoder to attend to\\n               all positions in the decoder up to and including that position. We need to prevent leftward\\n               information flow in the decoder to preserve the auto-regressive property. We implement this\\n               inside of scaled dot-product attention by masking out (setting to −∞) all values in the input\\n               of the softmax which correspond to illegal connections. See Figure 2.\\n\\n\\n 3.3      Position-wise Feed-Forward Networks\\n\\n\\n In addition to attention sub-layers, each of the layers in our encoder and decoder contains a fully\\n connected feed-forward network, which is applied to each position separately and identically. This\\n consists of two linear transformations with a ReLU activation in between.\\n\\n\\n                                                 FFN(x) = max(0, xW1 + b1)W2 + b2                                 (2)\\n\\n\\nWhile the linear transformations are the same across different positions, they use different parameters\\n from layer to layer. Another way of describing this is as two convolutions with kernel size 1.\\nThe dimensionality of input and output is dmodel = 512, and the inner-layer has dimensionality\\n df f = 2048.\\n\\n\\n 3.4      Embeddings and Softmax'"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Take a look at one of the source nodes from the response\n",
    "response_1.source_nodes[0].get_content()"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
